package com.gzedu.module.faced.user.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.gzedu.module.faced.user.UserFaced;
import com.gzedu.module.modle.user.User;
import com.gzedu.module.service.user.IUserService;
import com.gzedu.module.utils.IdGen;

import net.sf.json.JSONArray;

@Service
public class UserFacedImpl implements UserFaced{
	@Autowired
	private IUserService userService;
	
	/**
	 * 登录验证 (0 表示没有用户，1表示登录验证通过，2表示密码错误)
	 * @param userName
	 * @return
	 */
	public String login(User user){
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		User u = userService.getUserByName(user.getUserName());
		if(user != null){
			if(user.getPassword().equals(u.getPassword())){
				map.put("flag", 1);
			}else{
				map.put("flag", 2);
			}
		}else{
			map.put("flag", 0);
		}
		
		JSONArray json = JSONArray.fromObject(map);
		
		
		return json.toString();
	}

	public User getUserByName(String userName) {
		// TODO Auto-generated method stub
		return userService.getUserByName(userName);
	}

	public boolean save(User user) {
		// TODO Auto-generated method stub
		int res = 0;
		User userInfo = this.getUser(user);
		if(userInfo != null){
			res = userService.updateById(user);
		}else{
			user.setId(IdGen.creatUUID());
			res = userService.insert(user);
		}
		if(res == 1){
			return true;
		}else{
			return false;
		}
		
	}

	public User getUser(User user) {
		// TODO Auto-generated method stub
		return userService.getById(user.getId());
	}
}
